﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CollegeShop.Api.DBUtility;
using MySql.Data.MySqlClient;
using System.Data;

namespace CollegeShop.Api.Infrastructure
{
    /// <summary>
    /// 数据访问类:t_role
    /// </summary>
    public partial class T_ResourceDal
    {
        public T_ResourceDal()
        { }
        #region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId(int college)
        {
            return DbHelperMySQL.GetMaxID(college, "ID", "t_resource");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int college, int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from t_resource");
            strSql.Append(" where ID=@ID");
            MySqlParameter[] parameters = {
					new MySqlParameter("@ID", MySqlDbType.Int32)
			};
            parameters[0].Value = ID;

            return DbHelperMySQL.Exists(college, strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(int college, CollegeShop.Api.Entity.T_Resource model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_resource(");
            strSql.Append("path,client)");
            strSql.Append(" values (");
            strSql.Append("@path,@client)");
            MySqlParameter[] parameters = {
					new MySqlParameter("@path", MySqlDbType.VarChar,512),
                    new MySqlParameter("@client", MySqlDbType.Int32)};
            parameters[0].Value = model.Path;
            parameters[1].Value = model.Client;

            int rows = DbHelperMySQL.ExecuteSql(college, strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(int college, CollegeShop.Api.Entity.T_Resource model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_resource set ");
            strSql.Append("path=@path,client=@client");
            strSql.Append(" where ID=@ID");
            MySqlParameter[] parameters = {
					new MySqlParameter("@path", MySqlDbType.VarChar,512),
					new MySqlParameter("@client", MySqlDbType.Int32),
					new MySqlParameter("@ID", MySqlDbType.Int32,11)};
            parameters[0].Value = model.Path;
            parameters[1].Value = model.Client;
            parameters[2].Value = model.ID;

            int rows = DbHelperMySQL.ExecuteSql(college, strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int college, int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_resource ");
            strSql.Append(" where ID=@ID");
            MySqlParameter[] parameters = {
					new MySqlParameter("@ID", MySqlDbType.Int32)
			};
            parameters[0].Value = ID;

            int rows = DbHelperMySQL.ExecuteSql(college, strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CollegeShop.Api.Entity.T_Resource GetModel(int college, int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,Path,Client from t_resource ");
            strSql.Append(" where ID=@ID");
            MySqlParameter[] parameters = {
					new MySqlParameter("@ID", MySqlDbType.Int32)
			};
            parameters[0].Value = ID;

            CollegeShop.Api.Entity.t_role model = new CollegeShop.Api.Entity.t_role();
            DataSet ds = DbHelperMySQL.Query(college, strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CollegeShop.Api.Entity.T_Resource DataRowToModel(DataRow row)
        {
            CollegeShop.Api.Entity.T_Resource model = new CollegeShop.Api.Entity.T_Resource();
            if (row != null)
            {
                if (row["ID"] != null && row["ID"].ToString() != "")
                {
                    model.ID = int.Parse(row["ID"].ToString());
                }
                if (row["Path"] != null)
                {
                    model.Path = row["Path"].ToString();
                }
                if (row["Client"] != null)
                { 
                    int _client = 0;
                    Int32.TryParse(row["client"].ToString(), out _client);
                    model.Client = _client;
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(int college, string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,Path,Client ");
            strSql.Append(" FROM t_resource ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperMySQL.Query(college, strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(int college, string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM t_resource ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperMySQL.GetSingle(college, strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(int college, string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.ID desc");
            }
            strSql.Append(")AS Row, T.*  from t_resource T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperMySQL.Query(college, strSql.ToString());
        }

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}
